package com.presto.directo.mvvm.view

import android.os.Bundle
import android.view.*
import androidx.core.view.isInvisible
import com.fraggjkee.smsconfirmationview.SmsConfirmationView
import com.presto.directo.base.BaseActivity
import com.presto.directo.databinding.*
import com.presto.directo.mvvm.viewmodel.*
import com.presto.directo.net.*
import com.presto.directo.utils.*
import io.reactivex.rxjava3.core.Observable
import java.util.concurrent.TimeUnit

class LoginSmsActivity : BaseActivity() {

    private val binding by lazy { ActivityLoginSmsBinding.inflate(layoutInflater) }
    private val viewModel: LoginViewModel by lazy { getViewModel() }
    private val time = 60L
    private var showAmount = "20000"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Af.show_sms_login_page.track(this)
        val phone = intent.getStringExtra("phone") ?: ""
        interval(phone)
        viewModel.smsLiveData.observe(this) {
            interval(it)
        }
        binding.btnGetCode.setOnClickListener {
            Af.click_get_verify_code.track(this)
            viewModel.getSmsCode(phone)
        }
        binding.viewSms.onChangeListener = SmsConfirmationView.OnChangeListener { code, isComplete ->
            if (isComplete) {
                hideKeyBoard()
                viewModel.login(phone, code, this)
            }
        }
        start(onNext = {
            showAmount = (getApiService().getConfig(getHttpMap().sign(2)).result?.show_amount ?: "").ifEmpty { showAmount }
        }, toast = false)
    }

    private fun interval(phone: String) {
        binding.tvPhone.text = phonePrefix.plus(" ").plus(phone)
        Observable.interval(0, 1, TimeUnit.SECONDS)
            .take(time + 2)
            .applySchedulers()
            .getResponse(this, {
                val t = 60 - it
                binding.tvTime.text = t.toString()
                binding.btnGetCode.isInvisible = t >= 0
                binding.tvTime.isInvisible = t < 0
            })
    }

    override val rootView: View
        get() = binding.root

    override fun isTransparentStatusBar(): Boolean {
        return true
    }

    override fun isDarkStatusBar(): Boolean {
        return true
    }

    override fun back() {
        showConfirmDialog()
    }

    private fun showConfirmDialog() {
        showDialog { dialog ->
            val binding = LayoutCommonDialogBinding.inflate(layoutInflater)
            binding.tvContent.text = "¿Seguro de salir ? \nEstás a punto de perder la oportunidad de un préstamo hasta ＄$showAmount"
            binding.tvCancel.text = "Sí"
            binding.tvConfirm.text = "Solicita ahora"
            binding.tvCancel.setOnClickListener {
                dialog.dismiss()
                finish()
            }
            binding.tvConfirm.setOnClickListener { dialog.dismiss() }
            dialog.setContentView(binding.root)
        }
    }

    override fun onBackPressed() {
        back()
    }
}